/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.timsua.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import vn.timsua.model.Store;
import vn.timsua.utils.*;

/**
 *
 * @author Nguyen Manh Hung
 */
public class StoreDAOImpl implements IStoreDAO {

    public int insertStore(Store oStore) {
        String sSql = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Connection conn = null;
        int iResult = Constants.ERROR_FAILED;
        try {
            conn = DBPool.getConnection();
            conn.setAutoCommit(false);
            sSql = "INSERT INTO storeaccounts(email, password) VALUES(?,?)";
            pstmt = conn.prepareStatement(sSql);

            pstmt.setString(1, oStore.getEmail());
            pstmt.setString(2, MD5.Hash(oStore.getPassword()));

            if (pstmt.executeUpdate() != 1) {
                conn.rollback();
                System.out.println("Can't create store account!");
            } else {
                conn.commit();
                iResult = Constants.ERROR_OK;
            }
            
            // Đoạn này insert vào bảng storeinfo, chưa làm.
            if (iResult == Constants.ERROR_OK) {
                sSql = "INSERT INTO storeinfo() VALUES()";
            }

        } catch (Exception ex) {
            System.err.println("Exception in insertStore(): ");
            ex.printStackTrace();
        } finally {
            Util.closeConnection(rs, pstmt);
            DBPool.putConnection(conn);
        }
        return iResult;
    }
}
